/**
 * Demo1 场景：业务主键作为表的主键
 *   本示例中，name为实际表的主键，name在数据库和业务系统中都不能重复
 * @type {*[]}
 */
const dataSource = [
    {
        "name": "name1",
        "region": "shanghai",
        "date1": "2020-04-20 16:04:74",
        "delivery": true,
        "type": ["2", "1"],
        "resource": "1",
        "desc": "desc"
    },
    {
        "name": "name2",
        "region": "shanghai",
        "date1": "2020-04-20 17:04:74",
        "delivery": false,
        "type": ["1"],
        "resource": "1",
        "desc": "aa"
    }
];
export default {
    /**
     * 查询所有
     */
    'GET /api/delivery-1/list': {
        rtnCode: "0000000",
        rtnMesg: "success",
        oary: dataSource
    },
    /**
     * 新增
     * @param req
     * @param res
     * @returns {*|Promise<any>}
     * @constructor
     */
    'POST /api/delivery-1/add': (req, res) => {
        const payload = req.body;

        //模拟数据库主键重复
        if (dataSource.findIndex(d => d.name == payload.name) != -1) {
            return res.json({
                rtnCode: "3720002",
                rtnMesg: "重复的记录"
            })
        } else {

            //模拟数据库新增记录
            dataSource.push(payload);

            //返回
            return res.json({
                rtnCode: "0000000",
                rtnMesg: "success",
                oary: [payload]
            });
        }
    },
    /**
     * 修改记录
     * @param req
     * @param res
     * @returns {*|Promise<any>}
     */
    'PUT /api/delivery-1/update': (req, res) => {
        const payload = req.body;

        //根据主键name找到需要更新的记录
        let updateRowIndex = dataSource.findIndex(d => d.name == payload.name);

        //如果找不到报错
        if (updateRowIndex != -1) {
            return res.json({
                rtnCode: "3720003",
                rtnMesg: "找不到需要更新的记录"
            });
        } else {
            //模拟更新数据库对应记录
            dataSource[updateRowIndex] = payload;

            //数据库已成功更新，将结果返回给前端页面(其实结果和输入的是一样的,这里的返回只是为了符合restful标准)
            return res.json({
                rtnCode: "0000000",
                rtnMesg: "success",
                oary: [payload]
            });
        }
    },
    /**
     * 删除方法:可以只传name(推荐)，也可以传整个对象
     * @param req
     * @param res
     */
    'DELETE /api/delivery-1/update': (req, res) => {
        const payload = req.body;

        //根据主键name找到需要更新的记录
        let deleteRowIndex = dataSource.findIndex(d => d.name == payload.name);

        //如果找不到报错
        if (deleteRowIndex != -1) {
            return res.json({
                rtnCode: "3720004",
                rtnMesg: "找不到需要删除的记录"
            });
        } else {
            //模拟数据库删除记录
            dataSource.splice(deleteRowIndex, 1);
            return res.json({
                rtnCode: "0000000",
                rtnMesg: "success"
            });
        }
    }
}